package mapper;

import db.User;
import mysql.JDBCUtil;
import result.CodeMsg;

import java.awt.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author 计算机系 ITAEM 摆渡人
 * @description 用户表连接
 * @date 2022/9/20 10:00
 */
public class UserMapper extends JDBCUtil {

    //添加用户
    public CodeMsg insert(User user) {
        if (isExistence(user)) {
            closeAll();
            return CodeMsg.HAVE_EXISTENCE_ERROR;
        }
        int result = 0;

        String insertSQL = "INSERT INTO USER VALUES (NULL,?,?,0,0,0)";
        PreparedStatement statement = getStatement(insertSQL);
        try {
            statement.setObject(1, user.getUsername());
            statement.setObject(2, user.getPassword());
            result = statement.executeUpdate();
            if (result > 0) {
                return CodeMsg.SUCCESS;
            } else {
                return CodeMsg.DEFAULT;
            }
        } catch (SQLException e) {
            System.out.println("服务器错误——添加失败");
            return CodeMsg.SERVICE_ERROR;
        } finally {
            closeAll();
        }
    }

    //登录
    public CodeMsg login(User user) {
        if(!isExistence(user)){
            closeAll();
            return CodeMsg.NOT_EXISTENCE_ERROR;
        }

        String loginSql = "select * from user where username =? and password =?";
        PreparedStatement statement = getStatement(loginSql);
        boolean result = false;

        try {
            statement.setObject(1,user.getUsername());
            statement.setObject(2,user.getPassword());
            ResultSet resultSet = statement.executeQuery();
            result = resultSet.next();
            closeAll();
            if(result){
                return CodeMsg.SUCCESS;
            }
        } catch (SQLException e){
            System.out.println("服务器错误--登录错误");
            return CodeMsg.SERVICE_ERROR;
        } finally {
            closeAll();
        }
        return CodeMsg.PASSWORD_ERROR;
    }

    //判断用户是否存在
    public boolean isExistence(User user) {
        String existenceSql = "select * from user where username =?" ;
        PreparedStatement statement = getStatement(existenceSql);
        boolean result = false;
        try {
            statement.setObject(1,user.getUsername());
            ResultSet resultSet = statement.executeQuery();
            result = resultSet.next();
        } catch (SQLException e) {
            System.out.println("服务器错误--存在错误");
        } finally {
            closeAll();
        }
        closeAll();
        return result;
    }

    //获取排名前十的用户信息
    public ArrayList<String[]> page(){
        String pageSQL = "SELECT username,win,lost,total FROM user ORDER BY total DESC LIMIT 0,10";
        PreparedStatement statement = getStatement(pageSQL);
        try {
            ResultSet resultSet = statement.executeQuery();
            ArrayList<String[]> list = new ArrayList<>();
            list.add(new String[]{"序号", "用户名", "胜", "负","总积分"});
            for(int i = 1; resultSet.next(); i ++){
                list.add(new String[]{i+"",resultSet.getString("username"),resultSet.getInt("win")+"",
                        resultSet.getInt("lost")+"",resultSet.getInt("total")+""});
            }
            return list;
        } catch (SQLException sqlException){
            System.out.println("服务器错误--page");
            sqlException.printStackTrace();
            return null;
        }
    }

    //胜利
    public void updateWin(String username){
        String updateSQL = "UPDATE user SET win = win + 1,total = 10 * win - 5 * lost WHERE USERNAME = ?";
        PreparedStatement statement = getStatement(updateSQL);
        try {
            statement.setObject(1,username);
            statement.execute();
        } catch (SQLException sqlException){
            System.out.println("服务器错误--updateWin");
            sqlException.printStackTrace();
        }
    }

    //输
    public void updateLost(String username){
        String updateSQL = "UPDATE user SET lost = lost + 1,total = 10 * win - 5 * lost WHERE USERNAME = ?";
        PreparedStatement statement = getStatement(updateSQL);
        try {
            statement.setObject(1,username);
            statement.execute();
        } catch (SQLException sqlException){
            System.out.println("服务器错误--updateLost");
            sqlException.printStackTrace();
        }
    }
}
